package cn.signalpha.modules.shop.controller.admin;

import cn.hutool.core.date.DateUtil;
import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.log.exception.ServiceException;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.shop.dto.PublicityDTO;
import cn.signalpha.modules.shop.entity.Publicity;
import cn.signalpha.modules.shop.service.PublicityGoodsService;
import cn.signalpha.modules.shop.service.PublicityService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/admin/publicity")
@Api(value = "营销推广", tags = "营销推广")
public class PublicityController extends BaseController {

	private final PublicityService publicityService;
	private final PublicityGoodsService publicityGoodsService;

    @GetMapping("/page")
    @ApiOperation(value = "分页", notes = "")
    public R<IPage<Publicity>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
        IPage<Publicity> page = publicityService.page(Condition.getPage(query), Wrappers.<Publicity>query().lambda()
            .eq(ObjectUtil.isNotEmpty(params.get("type")), Publicity::getType, params.get("type"))
			.like(ObjectUtil.isNotEmpty(params.get("title")), Publicity::getTitle, params.get("title"))
			.eq(ObjectUtil.isNotEmpty(params.get("sellerId")), Publicity::getSellerId, params.get("sellerId"))
			.ge(ObjectUtil.isNotEmpty(params.get("beginTimeBegin")), Publicity::getBeginTime, params.get("beginTimeBegin"))
			.le(ObjectUtil.isNotEmpty(params.get("beginTimeEnd")), Publicity::getBeginTime, params.get("beginTimeEnd"))
			.ge(ObjectUtil.isNotEmpty(params.get("endTimeBegin")), Publicity::getEndTime, params.get("endTimeBegin"))
			.le(ObjectUtil.isNotEmpty(params.get("endTimeEnd")), Publicity::getEndTime, params.get("endTimeEnd"))
			.orderByDesc(Publicity::getId)
        );
        return R.data(page);
    }

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<Publicity> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(publicityService.getById(id));
	}

    @PostMapping("/save")
    @ApiOperation(value = "保存", notes = "传入publicity")
	public R save(@Valid @RequestBody PublicityDTO.SaveDTO publicityDTO) {
		if(ObjectUtil.isEmpty(publicityDTO.getSellerId())){
			publicityDTO.setSellerId(0L);
		}
		Publicity publicity = Objects.requireNonNull(BeanUtil.copy(publicityDTO, Publicity.class));
		publicity.setCreateTime(new Date());
		if(DateUtil.compare(publicityDTO.getBeginTime() , publicityDTO.getEndTime()) >= 0){
			throw new ServiceException("开始时间不能大于结束时间");
		}
		if(DateUtil.compare(new Date() , publicityDTO.getEndTime()) > 0){
			throw new ServiceException("结束时间必须大于当前时间");
		}
        return R.status(publicityService.save(publicity));
    }

	@PostMapping("/update")
	@ApiOperation(value = "更新", notes = "传入publicity")
	public R update(@Valid @RequestBody PublicityDTO.UpdateDTO publicityDTO) {
		return R.status(publicityService.updateById(BeanUtil.copy(publicityDTO, Publicity.class)));
	}

	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		Func.toLongList(ids).forEach(id -> {
			publicityService.removeById(id);
			publicityGoodsService.removeByPublicityId(id);
		});
		return R.status(true);
	}

}
